<?
if(!check_perms('site_torrents_notify')) { error(403); }

define('NOTIFICATIONS_PER_PAGE', 50);
list($Page,$Limit) = page_limit(NOTIFICATIONS_PER_PAGE);

$Results = $DB->query("SELECT SQL_CALC_FOUND_ROWS
		t.ID,
		g.ID,
		g.Name,
		g.CategoryID,
		g.TagList,
		t.Format,
		t.Encoding,
		t.Media,
		t.Scene,
		t.RemasterYear,
		g.Year,
		t.RemasterTitle,
		t.Snatched,
		t.Seeders,
		t.Leechers,
		t.Time,
		unt.UnRead
		FROM users_notify_torrents AS unt
		JOIN torrents AS t ON t.ID=unt.TorrentID
		JOIN torrents_group AS g ON g.ID = t.GroupID 
		WHERE unt.UserID='$LoggedUser[ID]'
		ORDER BY t.ID DESC LIMIT $Limit");
$DB->query('SELECT FOUND_ROWS()');
list($TorrentCount) = $DB->next_record();

//Clear before header but after query so as to not have the alert bar on this page load
$DB->query("UPDATE users_notify_torrents SET UnRead='0' WHERE UserID=".$LoggedUser['ID']);
$Cache->delete_value('notifications_new_'.$LoggedUser['ID']);
show_header('My notifications');

$DB->set_query_id($Results);

$Pages=get_pages($Page,$TorrentCount,NOTIFICATIONS_PER_PAGE,9);



?>
<h2>Latest notifications <a href="torrents.php?action=notify_clear&amp;auth=<?=$LoggedUser['AuthKey']?>">(clear all)</a> <a href="user.php?action=notify">(edit filters)</a></h2>
<div class="linkbox">
	<?=$Pages?>
</div>
<table class="border">
	<tr class="colhead">
		<td></td>
		<td><strong>Name</strong></td>
		<td><strong>Added</strong></td>
		<td style="text-align:right"><img src="static/styles/<?=$LoggedUser['StyleName']?>/images/snatched.png" alt="Snatches" title="Snatches" /></td>
		<td style="text-align:right"><img src="static/styles/<?=$LoggedUser['StyleName']?>/images/seeders.png" alt="Seeders" title="Seeders" /></td>
		<td style="text-align:right"><img src="static/styles/<?=$LoggedUser['StyleName']?>/images/leechers.png" alt="Leechers" title="Leechers" /></td>
	</tr>
<? if($DB->record_count()==0) { ?>
	<tr class="rowb">
		<td colspan="6" class="center">
			No new notifications found! <a href="user.php?action=notify">Edit notification filters</a>
		</td>
	</tr>
<? } else {
	$i = 0;
	while(list($TorrentID,$GroupID,$GroupName,$GroupCategoryID,$TorrentTags,$Format,$Encoding,
				$Media,$Scene,$RemasterYear,$GroupYear,$RemasterTitle,$Snatched,$Seeders,$Leechers,$NotificationTime,$UnRead) = $DB->next_record()) {	
		$i++;
		
		// generate torrent's title
		$DisplayName='';
		
		$Artists = get_artist($GroupID);
		
		if(!empty($Artists)) {
			$DisplayName = display_artists($Artists, true, true);
		}
		
		$DisplayName.= "<a href='torrents.php?id=$GroupID&amp;torrentid=$TorrentID'  title='View Torrent'>".$GroupName."</a>";

		if($GroupCategoryID==1 && $GroupYear>0) {
			$DisplayName.= " [$GroupYear]";
		}

		// append extra info to torrent title
		$ExtraInfo='';
		$AddExtra='';
		if($Format) 		{ $ExtraInfo.=$Format; $AddExtra=' / '; }
		if($Encoding) 		{ $ExtraInfo.=$AddExtra.$Encoding; $AddExtra=' / '; }
		if($Media) 		{ $ExtraInfo.=$AddExtra.$Media; $AddExtra=' / '; }
		if($Scene) 		{ $ExtraInfo.=$AddExtra.'Scene'; $AddExtra=' / '; }
		if($Year>0) 		{ $ExtraInfo.=$AddExtra.$Year; $AddExtra=' '; }
		if($RemasterTitle) 	{ $ExtraInfo.=$AddExtra.$RemasterTitle; }
		if($ExtraInfo!='') 	{
			$ExtraInfo = "- [$ExtraInfo]";
		}
		
		$TagList=array();
		if($TorrentTags!='') {
			$TorrentTags=explode(' ',$TorrentTags);
			foreach ($TorrentTags as $TagKey => $TagName) {
				$TagName = str_replace('_','.',$TagName);
				$TagList[]='<a href="torrents.php?taglist='.$TagName.'">'.$TagName.'</a>';
			}
			$TagList = implode(', ', $TagList);
			$TorrentTags='<br /><div class="tags">'.$TagList.'</div>';
		}

		// print row
?>
	<tr class="group_torrent">
		<td class="center"><img src="<?=STATIC_SERVER?>common/caticons/<?=$CategoryIcons[$GroupCategoryID-1]?>" /></td>
		<td>
			<span>[<a href="torrents.php?action=download&amp;id=<?=$TorrentID?>" title="Download">DL</a>]</span>
			<strong><?=$DisplayName?></strong> <?=$ExtraInfo ?>
			<? if($UnRead) { echo '<strong>New!</strong>'; } ?>
			<?=$TorrentTags?>
		</td>
		<td style="text-align:right" class="nobr"><?=time_diff($NotificationTime)?></td>
		<td style="text-align:right"><?=$Snatched ?></td>
		<td style="text-align:right"><?=$Seeders ?></td>
		<td style="text-align:right"><?=$Leechers ?></td>
	</tr>
<?
	}
}

?>
</table>
<div class="linkbox">
	<?=$Pages?>
</div>

<?
show_footer();
?>
